using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using VisualizationPackage.VectorField;
using VisualizationPackage.FileLoader;
using VisualizationPackage;
using Visualization;

namespace VisualUI
{
    public partial class frmVectorField : Form
    {
        private double _startX;
        public double StartX
        {
            get { return _startX; }
        }
        private double _startY;
        public double StartY
        {
            get { return _startY; }
        }
        private double _startZ;
        public double StartZ
        {
            get { return _startZ; }
        }
        private int _uField;
        public int UField
        {
            get { return _uField; }
        }
        private int _vField;
        public int VField
        {
            get { return _vField; }
        }
        private int _wField;

        public int WField
        {
            get { return _wField; }
        }

        private ReadOnlyCollection<string> _dataSource;

        public ReadOnlyCollection<string> DataSource
        {
            get { return _dataSource; }
            set
            {
                _dataSource = value;
                cmbUField.DataSource = new List<string>(value);
                cmbVField.DataSource = new List<string>(value);
                cmbWField.DataSource = new List<string>(value);
            }
        }
        private Mesh _mesh;

        public Mesh Mesh
        {
            get { return _mesh; }
            set { _mesh = value; }
        }
        private StreamLine _selectedStreamLine;
        public frmVectorField()
        {
            InitializeComponent();
        }

        private void frmVectorField_Load(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void RefreshList()
        {
            lstStreams.Items.Clear();
            foreach (StreamLine sLine in StreamLine.StreamLines)
            {
                string str = sLine.ToString();
                if (sLine.Parent != null && sLine.Parent is StreamRibon)
                    str = "(R) " + str;
                lstStreams.Items.Add(str);
            }
            if (_selectedStreamLine != null)
                lblSelected.Text = _selectedStreamLine.ToString();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_selectedStreamLine == null)
                Add();
            DialogResult = DialogResult.OK;

            Close();
        }

        private StreamLine Add()
        {
            try
            {
                _startX = double.Parse(txtStartX.Text);
                _startY = double.Parse(txtStartY.Text);
                _startZ = double.Parse(txtStartZ.Text);
            }
            catch
            {
                MessageBox.Show("Please enter valid floating point numbers in order to continue");
            }

            _uField = cmbUField.SelectedIndex;
            _vField = cmbVField.SelectedIndex;
            _wField = cmbWField.SelectedIndex;

            VisualizationPackage.FileLoader.Vertex vStart = new VisualizationPackage.FileLoader.Vertex();
            vStart.X = StartX;
            vStart.Y = StartY;
            vStart.Z = StartZ;

            StreamLine slines = new StreamLine(vStart, _uField, _vField, _wField, 0.01, Mesh);
            _selectedStreamLine = slines;
            StreamLine.StreamLines.Add(slines);
            return slines;
        }

        private void lnkCancel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.Cancel;

            Close();
        }

        private void lnkRemove_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (lstStreams.SelectedIndex != -1)
            {
                DialogResult dlgRes = MessageBox.Show("Are you sure you want to delete this StreamLine?", "Confirmation", MessageBoxButtons.YesNo);
                if (dlgRes == DialogResult.Yes)
                {
                    StreamLine.StreamLines.RemoveAt(lstStreams.SelectedIndex);
                    //lstStreams.Items.RemoveAt(lstStreams.SelectedIndex);
                    RefreshList();
                }
            }
        }

        private void lstStreams_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstStreams.SelectedIndex >= 0)
            {
                StreamLine strLine = StreamLine.StreamLines[lstStreams.SelectedIndex];
                txtStartX.Text = strLine.Start.X.ToString();
                txtStartY.Text = strLine.Start.Y.ToString();
                txtStartZ.Text = strLine.Start.Z.ToString();

                cmbUField.SelectedIndex = strLine.UVector;
                cmbVField.SelectedIndex = strLine.VVector;
                cmbWField.SelectedIndex = strLine.WVector;

                _selectedStreamLine = strLine;
                lblSelected.Text = strLine.ToString();
            }
        }

        private void lnkAddAnother_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (_selectedStreamLine == null)
                Add();
            //lstStreams.Items.Add(sLine.ToString());
            RefreshList();
        }

        private void lstStreams_Click(object sender, EventArgs e)
        {
            
        }

        private void lstStreams_MouseDown(object sender, MouseEventArgs e)
        {
        }

        private void lnkNew_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            lstStreams.SelectedIndex = -1;
            _selectedStreamLine = null;
            lblSelected.Text = "None";
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstStreams.SelectedIndex != -1)
            {
                DialogResult dlgRes = MessageBox.Show("Are you sure you want to delete this StreamLine?", "Confirmation", MessageBoxButtons.YesNo);
                if (dlgRes == DialogResult.Yes)
                {
                    StreamLine.StreamLines.RemoveAt(lstStreams.SelectedIndex);
                    //lstStreams.Items.RemoveAt(lstStreams.SelectedIndex);
                    RefreshList();
                }
            }
        }

        private void addRemoveStreamRibonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstStreams.SelectedIndex != -1)
            {
                if (StreamLine.StreamLines[lstStreams.SelectedIndex].Parent == null)
                {
                    StreamRibon stRibon = new StreamRibon(StreamLine.StreamLines[lstStreams.SelectedIndex]);
                    StreamRibon.StreamRibons.Add(stRibon);
                    lstStreams.Items[lstStreams.SelectedIndex] = "(R) " + ((string)lstStreams.Items[lstStreams.SelectedIndex]);
                }
                else
                {
                    lstStreams.Items[lstStreams.SelectedIndex] = StreamLine.StreamLines[lstStreams.SelectedIndex].ToString();
                    int ind = StreamRibon.StreamRibons.IndexOf(StreamLine.StreamLines[lstStreams.SelectedIndex].Parent as StreamRibon);
                    if (ind != -1)
                        StreamRibon.StreamRibons.RemoveAt(ind);
                    StreamLine.StreamLines[lstStreams.SelectedIndex].Parent = null;
                }
            }
        }
    }
}